Telegram Group & Telegram Channel
📊 Шпаргалка по Python-профайлингу: как найти узкие места в коде

Разбираем 4 мощных инструмента для анализа производительности и памяти в Python.

🧠 `cProfile` — встроенный профайлер времени

Показывает, сколько времени тратится на каждую функцию
Идеально для быстрого анализа


python -m cProfile your_script.py


Форматированный вывод с pstats:


python -m cProfile -o result.prof your_script.py
python -m pstats result.prof


Для Jupyter:


%load_ext cprofile
%cprofile some_function()


🐍 py-spy — суперлёгкий sampling-профайлер

Не требует изменений в коде
Работает с чужими процессами
Без тормозов, можно запускать в проде


py-spy top --pid <PID>
py-spy record -o profile.svg -- python your_script.py


👉 Показывает flame graph: удобная визуализация bottleneck-функций.

🔥 `Scalene` — профайлинг CPU, памяти и аллокаций

Отслеживает:
- сколько времени тратит CPU
- где происходят аллокации
- сколько памяти реально используется


pip install scalene
scalene your_script.py


👉 Выделяет проблемные строки, показывает выделение памяти по строчкам кода, а не только по функциям.

🧮 `memory_profiler` — анализ использования памяти

Показывает, сколько памяти потребляет каждая строка
Полезен для data science скриптов


pip install memory-profiler


Добавь декоратор:


from memory_profiler import profile

@profile
def my_func():
...


Запуск:


python -m memory_profiler your_script.py


💡 Как выбрать?

| Инструмент | Что профилирует | Подходит для |
|--------------------|----------------------|------------------------------|
| `cProfile` | Время (встроенно) | Быстрый старт, базовый анализ |
| `py-spy` | Время (sampling) | Прод, чужие процессы, flame graphs |
| `Scalene` | Время + память + аллокации | Глубокий анализ по строкам |
| `memory_profiler` | Только память | Data science, отладка RAM |



tg-me.com/pro_python_code/1823
Create:
Last Update:

📊 Шпаргалка по Python-профайлингу: как найти узкие места в коде

Разбираем 4 мощных инструмента для анализа производительности и памяти в Python.

🧠 `cProfile` — встроенный профайлер времени

Показывает, сколько времени тратится на каждую функцию
Идеально для быстрого анализа


python -m cProfile your_script.py


Форматированный вывод с pstats:


python -m cProfile -o result.prof your_script.py
python -m pstats result.prof


Для Jupyter:


%load_ext cprofile
%cprofile some_function()


🐍 py-spy — суперлёгкий sampling-профайлер

Не требует изменений в коде
Работает с чужими процессами
Без тормозов, можно запускать в проде


py-spy top --pid <PID>
py-spy record -o profile.svg -- python your_script.py


👉 Показывает flame graph: удобная визуализация bottleneck-функций.

🔥 `Scalene` — профайлинг CPU, памяти и аллокаций

Отслеживает:
- сколько времени тратит CPU
- где происходят аллокации
- сколько памяти реально используется


pip install scalene
scalene your_script.py


👉 Выделяет проблемные строки, показывает выделение памяти по строчкам кода, а не только по функциям.

🧮 `memory_profiler` — анализ использования памяти

Показывает, сколько памяти потребляет каждая строка
Полезен для data science скриптов


pip install memory-profiler


Добавь декоратор:


from memory_profiler import profile

@profile
def my_func():
...


Запуск:


python -m memory_profiler your_script.py


💡 Как выбрать?

| Инструмент | Что профилирует | Подходит для |
|--------------------|----------------------|------------------------------|
| `cProfile` | Время (встроенно) | Быстрый старт, базовый анализ |
| `py-spy` | Время (sampling) | Прод, чужие процессы, flame graphs |
| `Scalene` | Время + память + аллокации | Глубокий анализ по строкам |
| `memory_profiler` | Только память | Data science, отладка RAM |

BY Python RU


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/pro_python_code/1823

View MORE
Open in Telegram


Python RU Telegram | DID YOU KNOW?

Date: |

Telegram has exploded as a hub for cybercriminals looking to buy, sell and share stolen data and hacking tools, new research shows, as the messaging app emerges as an alternative to the dark web.An investigation by cyber intelligence group Cyberint, together with the Financial Times, found a ballooning network of hackers sharing data leaks on the popular messaging platform, sometimes in channels with tens of thousands of subscribers, lured by its ease of use and light-touch moderation.

The messaging service and social-media platform owes creditors roughly $700 million by the end of April, according to people briefed on the company’s plans and loan documents viewed by The Wall Street Journal. At the same time, Telegram Group Inc. must cover rising equipment and bandwidth expenses because of its rapid growth, despite going years without attempting to generate revenue.

Python RU from us


Telegram Python RU
FROM USA